<template>
  <div style="height: 100%;width: 100%">
    <el-container class="index">
      <el-menu
        :default-active="$route.path"
        :unique-opened="true"
        class="el-menu-vertical-demo"
        background-color="#FFF"
        text-color="#000000"
      >
        <StrongMenu class="menu" :MenuData="menuItems"></StrongMenu>
      </el-menu>
      <el-container class="el-container">
        <el-header class="headers">
          <div class="box-card">
            <div class="label">
              <nav class="div-tags">
                <ul class="ul-c">
                  <li
                    v-for="(item, index) in tagsArry"
                    :class="{ active: isActive(item.name) }"
                    @click="activeTag(index)"
                    :key="index"
                  >
                    <a class="li-a">{{ item.name }}</a>
                    <svg-icon style="font-size: 16px;" @click="closeTag(index)" icon-class="叉"></svg-icon>
                  </li>
                </ul>
              </nav>
            </div>
            <div class="div2">
              <div class="msgFrame" style="background-color: #42b983;width: 20px;height: 20px"></div>
              <span>
                <el-badge :value="12" class="item">
                  <svg-icon class="msg" @click="selMsg" style="font-size: 25px" icon-class="消息"></svg-icon>
                </el-badge>
              </span>
            </div>
            <div class="div1">
              <!--              <span style="font-size: 20px">{{ userName }}</span>-->
              <el-dropdown @command="handleCommand">
                <span class="el-dropdown-link">
                  <svg-icon style="font-size: 35px" icon-class="用户头像"></svg-icon>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="setup">设置</el-dropdown-item>
                  <el-dropdown-item command="logout">注销</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
          <el-dialog title="设置" :visible.sync="dialogTableVisible">
            <el-table :data="gridData">
              <el-table-column
                property="date"
                label="日期"
                width="150"
              ></el-table-column>
              <el-table-column
                property="name"
                label="姓名"
                width="200"
              ></el-table-column>
              <el-table-column property="address" label="地址"></el-table-column>
            </el-table>
            <el-button type="primary">确认修改</el-button>
          </el-dialog>
        </el-header>
        <el-main class="main">
          <keep-alive :include="keepAliveData">
            <router-view name="home" />
          </keep-alive>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import StrongMenu from '../components/strongMenu'
import svgIcon from "../components/SvgIcon/index.vue";

export default {
  name: "index",
  data() {
    return {
      dialogTableVisible: false,
      name: this.$route.name,
      userName: sessionStorage.getItem("userName"),
      gridData: [],
      active: "1",
      tagsArry: [],
      isCollapse: true,
      fullscreen: false,
    }
  },
  components: {StrongMenu, svgIcon},
  computed: {
    menuItems() {
      return this.$store.state.menuItems;
    },
    keepAliveData() {
      if (this.tagsArry == null) {
        const name = this.$route.name;
        this.tagsArry = [];
        this.tagsArry.push({
          text: this.nameToTitle[name],
          name: name,
        });
      }
      return this.tagsArry.map((item) => item.name);
    },
  },
  watch: {
    $route(to) {
      const name = to.name;
      if (name == "error") {
        this.crumbs = "404";
        return;
      }
      if (!this.keepAliveData.includes(name)) {
        // 如果标签超过8个 则将第一个标签删除
        if (this.tagsArry.length == 6) {
          this.tagsArry.shift();
        }
        this.tagsArry.push({name, text: this.nameToTitle[name]});
      }
    },
    tagsArry(newProp, oldProp) {
      sessionStorage.setItem("data", JSON.stringify(newProp));
    },
  },
  mounted() {
    this.tagsArry.push({
      text: this.nameToTitle[name],
      name: name,
    });
    this.tagsArry = JSON.parse(sessionStorage.getItem("data"));
  },
  methods: {
    handleCommand(command) {
      if (command == "logout") {
        sessionStorage.removeItem("token");
        sessionStorage.removeItem("userName");
        this.$router.push({
          path: "/login",
        });
      } else if (command == "setup") {
        this.dialogTableVisible = true;
      }
    },
    // 判断当前标签页是否激活状态
    isActive(name) {
      return this.$route.name === name;
    },
    // 定义组件名称和标签栏标题的映射表 有多少个页面就有多少个映射条数
    nameToTitle() {
      const obj = {};
      this.menuItems.forEach((e) => {
        this.processNameToTitle(obj, e);
      });
      return obj;
    },
    processNameToTitle(obj, data, text) {
      if (data.name) {
        obj[data.name] = {
          name: data.name,
          type: data.type,
          text: data.text,
        };
        this.paths[data.name] = text ? `${text} / ${data.text}` : data.text;
      }
      if (data.children) {
        data.children.forEach((e) => {
          this.processNameToTitle(
            obj,
            e,
            text ? `${text} / ${data.text}` : data.text
          );
        });
      }
    },
    // 跳转页面 路由名称和参数
    gotoPage(name, params) {
      let name2 = this.$route.name;
      if (name2 == name) {
        return
      } else {
        this.$router.replace({name, params});
      }
      if (!this.keepAliveData.includes(name)) {
        // 如果标签超过8个 则将第一个标签删除
        if (this.tagsArry.length === 6) {
          this.tagsArry.shift();
        }
        this.tagsArry.push({name, text: this.nameToTitle[name]});
      }
    },
    // 激活标签
    activeTag(i) {
      this.gotoPage(this.tagsArry[i].name);
    },
    // 关闭单个标签
    closeTag(i) {
      let name = this.tagsArry[i].name;
      this.tagsArry.splice(i, 1);
      event.stopPropagation();
      // 如果关闭的是当前标签 则激活前一个标签
      // 如果关闭的是第一个标签 则激活后一个标签
      if (this.tagsArry.length) {
        if (this.isActive(name)) {
          if (i != 0) {
            this.gotoPage(this.tagsArry[i - 1].name);
          } else {
            this.gotoPage(this.tagsArry[i].name);
          }
        }
      } else {
        // 如果没有标签则跳往首页
        this.$router.push({
          path: "/",
        });
      }
    },

    //打开消息列表
    selMsg(){

    }
  }
}
</script>

<style scoped>
.headers {
  width: 100%;
  position: relative;
  background-color: white;
  border-radius: 5px;
  box-shadow: 0px 3px 5px #bdbdbd !important;
}

.index {
  width: 100%;
  height: 100%;
  background: #eee !important;
}

.box-card {
  height: 100%;
  width: 100%;
}

.div1 {
  position: absolute;
  top: 23%;
  right: 2%;
  display: flex;
}

.div2 {
  position: absolute;
  /*background-color: black;*/
  top: 35%;
  right: 10%;
}

.label {
  width: 100%;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;

}

.div-tags {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /*box-shadow: 0px 1px 4px rgba(0, 0, 0, 0.3),*/
  /*0px 0px 20px rgba(0, 0, 0, 0.1) inset;*/
}

.ul-c {
  height: 34px;
  margin-top: 2px;
  background: #fff;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 0 10px;
  overflow: hidden;
  width: 100%;
  color: #fff;
  padding: 10px;
}

.ul-c li {
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
  height: 24px;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 3px 5px 2px 3px;
  border: 1px solid #e6e6e6;
  background: #0BB278;
  color: #fff;
  transition: all 0.3s;
}

.ul-c li:after {
  content: "";
  display: block;
  position: absolute;
  top: -10px;
  left: -10px;
  right: -10px;
  bottom: -10px;
  pointer-events: none;
  background-color: #888;
  background-repeat: no-repeat;
  background-position: 50%;
  opacity: 0;
  transition: all 0.3s;
}

.li-a {
  color: #fff;
}

.ul-c li:hover {
  transition: all 0.3s;
  background: #0BB278;
}

.el-main {
  padding: 1px;
}

.svg-icon[data-v-c8a70580] {
  vertical-align: -0em !important;
}
.msgFrame{
  display:none;
}
.main{
  height: 100%;
  margin: 15px;
  background-color: #ffffff;
  border-radius: 5px;
  box-shadow: 0px 3px 5px #bdbdbd;
}
</style>
<style>
/*.msg:hover{*/
/*  .magFrame{*/
/*    display:inline;*/
/*  }*/

/*}*/
</style>
